package org.fh.inductionLamp.impl.request;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.fh.HandleData;
import org.fh.ServerApplication;
import org.fh.cmd.CommunicationCmd;
import org.fh.domain.DataFrame;
import org.fh.inductionLamp.api.LampRequest;
import org.fh.inductionLamp.api.LampResponse;
import org.fh.inductionLamp.impl.response.GetConfigResponse;
import org.fh.inductionLamp.impl.response.LampControlResponse;
import org.fh.utils.ConvertCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @Author: wwy
 * @Date: 2020/10/23 10:21
 */
public class LampControlRequest implements LampRequest {
    private static final Logger LOGGER = LoggerFactory.getLogger(LampControlRequest.class);
    private List<String> pDSata;
    private int length;
    @Override
    public LampControlResponse execute(int srcId, int destId) {
        //清空全局句柄
        HandleData.tcpDataHashMap.remove(String.valueOf(destId));
        DataFrame dataFrame = ServerApplication.sendData.sendData(CommunicationCmd.CMD_UPDATA_STATUS, srcId, pDSata, length);
        String message = ServerApplication.sendData.dataFrameConvert(dataFrame);
        LOGGER.info("服务端发送数据：{}",message);
        ByteBuf bufff = Unpooled.buffer();//netty需要用ByteBuf传输
        bufff.writeBytes(ConvertCode.hexString2Bytes(message));//对接需要16进制
        // 发送客户端的请求
        ChannelHandlerContext ctx = HandleData.channelHandlerContextMaps.get(String.valueOf(destId));
        ctx.writeAndFlush(bufff);
        LampControlResponse lampControlResponse = new LampControlResponse(String.valueOf(destId));
        return lampControlResponse;
    }

    public List<String> getpDSata() {
        return pDSata;
    }

    public void setpDSata(List<String> pDSata) {
        this.pDSata = pDSata;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
}
